<?php
declare (strict_types=1);

namespace app\common\controller;

use app\common\traits\ResponseTrait;
use think\App;
use think\Validate;
use think\exception\ValidateException;

/**
 * 控制器基础类
 */
abstract class BaseController
{
    use ResponseTrait;

    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 是否批量验证
     * @var bool
     */
    protected $batchValidate = false;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [];

    /**
     * 构造方法
     * @access public
     * @param App $app 应用对象
     */
    public function __construct(App $app)
    {
        $this->app = $app;
        $this->request = $this->app->request;

        // 控制器初始化
        $this->initialize();
    }

    // 初始化
    protected function initialize()
    {
    }

    /**
     * @notes post请求数据
     * @param null $validate
     * @param null $scene
     * @param bool $batch
     * @return array|mixed
     * @author mjf
     * @date 2022/4/14 17:01
     */
    protected function checkPost($validate = null, $scene = null, bool $batch = false)
    {
        return $this->params('POST', $validate, $scene, $batch);
    }

    /**
     * @notes get请求参数
     * @param null $validate
     * @param null $scene
     * @param bool $batch
     * @return array|mixed
     * @author mjf
     * @date 2022/4/14 17:01
     */
    protected function checkGet($validate = null, $scene = null, bool $batch = false)
    {
        return $this->params('GET', $validate, $scene, $batch);
    }

    /**
     * @notes 请求参数
     * @param string $method
     * @param null $validate
     * @param null $scene
     * @param bool $batch
     * @return array|mixed
     * @author mjf
     * @date 2022/4/14 17:00
     */
    protected function params(string $method, $validate = null, $scene = null, bool $batch = false)
    {
        if ($method == 'GET') {
            $params = request()->get();
        } else {
            $params = request()->post();
        }

        if (is_null($validate)) {
            return $params;
        }

        $this->checkRequestData($params, $validate, $scene, $batch);
        return $params;
    }

    /**
     * @notes 校验请求参数
     * @param array $data
     * @param $validate
     * @param null $scene
     * @param bool $batch
     * @return mixed
     * @author mjf
     * @date 2022/4/14 15:45
     * @remark 由原validate调整而来。去除$message，增加场景$scene。
     */
    protected function checkRequestData(array $data, $validate, $scene = null, bool $batch = false)
    {
        if (is_string($validate) && strpos($validate, '.')) {
            [$validate, $scene] = explode('.', $validate);
        }

        $class = false !== strpos($validate, '\\')
            ? $validate
            : $this->app->parseClass('validate', $validate);

        $v = new $class();

        if (!empty($scene)) {
            $v->scene($scene);
        }

        // 是否批量验证
        if ($batch || $this->batchValidate) {
            $v->batch(true);
        }

        return $v->failException(true)->check($data);
    }

    /**
     * 验证数据
     * @access protected
     * @param array $data 数据
     * @param string|array $validate 验证器名或者验证规则数组
     * @param array $message 提示信息
     * @param bool $batch 是否批量验证
     * @return array|string|true
     * @throws ValidateException
     */
    protected function validate(array $data, $validate, array $message = [], bool $batch = false)
    {
        if (is_array($validate)) {
            $v = new Validate();
            $v->rule($validate);
        } else {
            if (strpos($validate, '.')) {
                // 支持场景
                [$validate, $scene] = explode('.', $validate);
            }
            $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
            $v = new $class();
            if (!empty($scene)) {
                $v->scene($scene);
            }
        }

        $v->message($message);

        // 是否批量验证
        if ($batch || $this->batchValidate) {
            $v->batch(true);
        }

        return $v->failException(true)->check($data);
    }


}
